//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace CommunityServer.Blogs.Controls
{
	/// <summary>
	/// Summary description for IndividualSearchContainer.
	/// </summary>
	public class IndividualSearchContainer : WeblogThemedControl
	{
		public IndividualSearchContainer()
		{
			//
			// TODO: Add constructor logic here
			//
		}

        #region Private Members

        private EntryList SearchPostList;
        private IText Message;

        #endregion

        /// <summary>
        /// Gets or sets if the results are paged.
        /// </summary>
        [
        System.ComponentModel.DefaultValue( false ),
        ]
        public virtual Boolean EnablePaging 
        {
            get 
            {
                Object state = ViewState["EnablePaging"];
                if ( state != null ) 
                {
                    return (Boolean)state;
                }
                return false;
            }
            set 
            {
                ViewState["EnablePaging"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the size of a page when paging is enabled.
        /// </summary>
        [
        System.ComponentModel.DefaultValue( 15 ),
        ]
        public virtual Int32 PageSize 
        {
            get 
            {
                Object state = ViewState[ "PageSize" ];
                if ( state != null ) 
                {
                    return (Int32)state;
                }
                return WeblogConfiguration.Instance().IndividualPostCount;
            }
            set 
            {
                ViewState[ "PageSize" ] = value;
            }
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad (e);
            
            DataBind();

        }

        public override void DataBind()
        {
            base.DataBind ();

            CSContext context = CSContext.Current;

            if(!Globals.IsNullorEmpty(context.QueryText))
            {
                SearchQuery query = SearchQuery.SectionSearch(context.QueryText,CurrentWeblog.SectionID);
                query.PageSize = this.PageSize;
                query.PageIndex = context.PageIndex;

                
                SearchResultSet results = CSSearch.Search(query);

                if(results.Posts != null && results.Posts.Count > 0)
                {
                    SearchPostList.DataSource = results.Posts;
                    SearchPostList.TotalRecords = results.TotalRecords;
                    SearchPostList.EnablePaging = this.EnablePaging;
                    SearchPostList.PageIndex = query.PageIndex;
                    SearchPostList.PageSize = this.PageSize;
                    SearchPostList.DataBind();

                    Message.Text = string.Format(ResourceManager.GetString("Weblog_SearchResult_Message"),results.TotalRecords,results.SearchDuration);

                    this.SetPageTitle(string.Format(ResourceManager.GetString("Weblog_Individual_SearchResult_PageTitle"),context.QueryText));
                }
                else
                {
                    Message.Text = "No Results";
                }


            }
            else
            {
                Message.Visible = false;
            }
        }


	    protected override void AttachChildControls()
	    {
            SearchPostList = FindControl("SearchPostList") as EntryList;
            Message = FindText("Message");
	    }
	}
}
